function sortPage(obj) {
    let dom = "";
    for (let key in obj) {
        let arr = obj[key];
        if (arr.length == 0) {
            continue;
        }
        let str = `
            <h1>${key}</h1>
            <div class="d-flex flex-wrap">
        `;
        let str2 = (function () {
            let str = ""
            for (let value of arr) {
                str += `
                <div class="card" style="width: 21rem;">
                    <div class="card-body rounded">
                        <h5 class="card-title" id="link-card-name">${value.name}</h5>
                        <p class="card-text">
                        <ul>
                            <li id="link-card-id" style="display: none;">${value.id}</li>
                            <li><span class="item-label">分组</span><span class="item-content" id="link-card-type">${value.type.name}</span>
                            </li>
                            <li><span class="item-label">手机</span><span class="item-content" id="link-card-phone">${value.phone}</span></li>
                            <li><span class="item-label">邮箱</span><span class="item-content" id="link-card-email">${value.email}</span></li>
                        </ul>
                        </p>
                        <button type="button" class="btn btn-outline-success change-btn">change</button>
                        <button class="btn btn-outline-danger del-btn" btn-id="${value.id}">delete</button>
                    </div>
                </div>
                `;
            }
            return str;
        })();
        dom += (str + str2 + `</div>`);
    }
    $('main').html(dom);
}


function getSort() {
    $.ajax({
        type: 'GET',  // 请求方式（GET | POST）
        url: '/ContactPerson/getSort',    // 请求地址
        dataType: "json",
        // 请求成功执行此函数
        success: function (res) {
            sortPage(res);
            console.log("success")
        },
        // 请求失败执行此函数
        error: function () {
            console.log("failure");
        }
    });
}

$(function () {
    getSort();

    $('main').on('click', ".del-btn", function () {
        let id = $(this).attr("btn-id");
        $.ajax({
            type: 'GET',  // 请求方式（GET | POST）
            url: '/ContactPerson/del',    // 请求地址
            data: {
                "id": id
            },
            success: function () {
                getSort();
            }
        });
    });
});